﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Actions;

namespace Bing.Studio
{
    public partial class FrmInsertScript : BaseForm
    {
        private static int _index = 1;
        private string _strConnectionString;
        private string _strTableName;

        public FrmInsertScript(string strConnectionString, string strTableName)
        {
            _strConnectionString = strConnectionString;
            _strTableName = strTableName;

            InitializeComponent();
        }

        /****************************************************************************************************/

        #region /*自定义方法*/

        private void BuilderScript()
        {
            var db = new SQLServerDataStructuresHelper(_strConnectionString);
            try
            {
                var result = db.Execute("SELECT * FROM " + _strTableName);
                StringBuilder sbInserScript = new StringBuilder();
                if (result.Rows.Count > 0)
                {
                    int intColumnCount = result.Columns.Count;
                    string strColumn = String.Empty;
                    foreach (DataColumn entry in result.Columns)
                    {
                        strColumn += entry.ColumnName + ",";
                    }
                    strColumn = strColumn.Remove(strColumn.Length - 1);
                    foreach (DataRow entry in result.Rows)
                    {
                        sbInserScript.AppendFormat("INSERT INTO {0} (", _strTableName);
                        sbInserScript.Append(strColumn);
                        sbInserScript.Append(") VALUES (");
                        for (var i = 0; i < intColumnCount; i++)
                        {
                            sbInserScript.AppendFormat("'{0}',", entry[i].ToString());
                        }
                        sbInserScript.Remove(sbInserScript.Length - 1, 1);
                        sbInserScript.Append(")\r\n");
                    }
                    textEditorControl1.Text = sbInserScript.ToString();
                    SendNotification(String.Format("本次共生成({0})Insert脚本.", result.Rows.Count));
                }
            }
            catch (Exception ex)
            {
                SendNotification(ex);
            }
        }

        #endregion

        /****************************************************************************************************/

        private void FrmInsertScript_Load(object sender, EventArgs e)
        {
            this.textEditorControl1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("TSQL");
            this.textEditorControl1.Encoding = Encoding.Default;
            this.TabText = "脚本生成器_" + (_index++);

            BuilderScript();
        }

        /****************************************************************************************************/
    }
}
